import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/home/HomeView.vue'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [{
    path: '/',
    redirect: '/home'
  },
  {
    path: '/body',
    name: 'body',
    component: () => import('@/views/BodyView.vue'),
    children: [{
        path: '/home',
        name: 'home',
        component: HomeView
      },
      {
        path: '/list',
        name: 'list',
        component: () => import('@/views/list/ListView.vue')
      },
      {
        path: '/my',
        name: 'my',
        component: () => import('@/views/my/MyView.vue')
      },
      {
        path: '/search',
        component: () => import('@/views/home/search/Search.vue'),
        children: [{
            path: '/',
            name: 'search',
            component: () => import('@/views/home/search/SearchIndex.vue')
          },
          {
            path: 'list',
            name: 'search-list',
            component: () => import('@/views/home/search/SearchList.vue')
          }
        ]
      },
      {
        path: '/login',
        name: 'login',
        component: () => import('@/views/my/login/Login.vue')
      },
      {
        path: '/userLogin',
        name: 'userLogin',
        component: () => import('@/views/my/login/UserLogin.vue')
      },
      {
        path: '/register',
        name: 'register',
        component: () => import('@/views/my/login/Register.vue')
      },
      {
        path: '/recovery',
        component: () => import('@/views/my/recoveryPwd/Recovery.vue'),
        children: [{
          path: '/',
          name: 'recoveryPwd',
          component: () => import('@/views/my/recoveryPwd/RecoveryPwd.vue')
        }, {
          path: 'recoveryBtn',
          name: 'recoveryBtn',
          component: () => import('@/views/my/recoveryPwd/RecoveryBtn.vue'),
        }]
      }, {
        path: '/path',
        name: 'path',
        component: () => import('@/views/my/path/Path.vue')
      }, {
        path: '/setPath',
        name: 'setPath',
        component: () => import('@/views/my/path/SetPath.vue')
      }
    ]
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('@/views/home/detail/Detail.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/cart/CartView.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('@/views/my/order/Order.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  let nextRoute = ['Payment','Path','Order','setPath'];
  //是否是登录中
  let userInfo = JSON.parse( localStorage.getItem('teaUserInfo')  );
  //当前进入的页面，是不是需要验证的
  if(  nextRoute.indexOf( to.name ) >= 0  ){
      if( !userInfo ){
          router.push('/login');
      }
  }
  next();
})
export default router
